package com.server.rule.component.triple.simple;

import org.jdom.Element;

import com.server.rule.component.triple.Triple;
import com.server.rule.component.triple.operator.NominalOperator;
import com.server.rule.component.triple.operator.NominalOperatorEqual;
import com.server.rule.component.triple.operator.NominalOperatorNonEqual;
import com.shared.rule.TypeOperator;
import com.server.rule.data.Data;
import com.shared.rule.TypeDataRule;
import  com.server.rule.data.ExceptionTypeData;



public class NominalTriple extends SimpleTriple{

	
	@Override
	public boolean match(Data data) throws ExceptionTypeData {
		if(data.get(labelData1).getTypeData()!=TypeDataRule.NOMINAL) throw new ExceptionTypeData("Mauvais type de donnes pour "+labelData1+", Attendu:"+TypeDataRule.NOMINAL);
		if(data.get(labelData2).getTypeData()!=TypeDataRule.NOMINAL) throw new ExceptionTypeData("Mauvais type de donnes pour "+labelData2+", Attendu:"+TypeDataRule.NOMINAL);
		return ((NominalOperator) operator).compare((String) data.getValue(labelData1),(String) data.getValue(labelData2));
	}

	//@Override
	public boolean equals(Triple p) {
		if(!(p instanceof NominalTriple)) return false;
		return (labelData1.equals(((SimpleTriple) p).labelData1)) && (labelData2.equals(((SimpleTriple) p).labelData2)) && (operator.equals(((SimpleTriple) p).operator));
	}
	
	@Override
	public Element getXMLElement() {
		Element xmlElement = new Element("Triple");
		
		xmlElement.setAttribute("type","NominalTriple");
		xmlElement.setAttribute("label1",labelData1);
		xmlElement.setAttribute("label2",labelData2);
		xmlElement.setAttribute("operator",operator.toString());
		return xmlElement;
	}

	@Override
	public void generateFromXML(Element xmlElement) {		
		labelData1 = xmlElement.getAttributeValue("label1");
		labelData2 = xmlElement.getAttributeValue("label2");		
		if(xmlElement.getAttributeValue("operator").equals(TypeOperator.NOMINAL_EQUAL.toString())) operator = new NominalOperatorEqual();	
		if(xmlElement.getAttributeValue("operator").equals(TypeOperator.NOMINAL_NON_EQUAL.toString())) operator = new NominalOperatorNonEqual();
		
		
	}

		

}
